<template>
  <div>
    <div>
     <el-button>
          <div>
      <el-tooltip
        class="item"
        effect="dark"
        content="导入Excel"
        placement="left"
      >
        <el-button
          type="text"
          @click="importExcel"
          icon="el-icon-download"
        ></el-button>
      </el-tooltip>
      <ImportPointConfiguration
        ref="ImportWin"
        @ImportSuccess="ImportSuccess"
      ></ImportPointConfiguration>
    </div>
    <div>
      <el-form-item label="文件" prop="ValueType">
        <input id="file" type="file" class="file" />
      </el-form-item>
      <button type="primary" @click="save" size="small">导 入</button>
    </div>
     </el-button>
 


      <el-button type="primary">
        <download-excel
          type="primary"
          class="export-excel-wrapper"
          :data="json_data"
          :fields="json_fields"
          name="filename.xls"
        >
          导出excel表
        </download-excel>
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  props:{},
  data () {
    return {
           json_fields: {
        "工号": "jobnumber",    //常规字段
        "员工姓名（必填）": "name",   
        "手机号（必填）": "phone.mobile", //支持嵌套属性
        "部门": "department", //支持嵌套属性
        "职位": "post", //支持嵌套属性
        // "备用电话": {
        //   field: "phone.landline",
        //             //自定义回调函数
        //   callback: value => {
        //     return `Landline Phone - ${value}`;
        //   }
        // }
      },
      json_data: [
        {
          name: "魏佳",
          jobnumber: "2020001",
          post: "前端工程师",
          department: "开发部",
          phone: {
            mobile: "15701793476",
            landline: "13281357561"
          }
        },
      ],
      json_meta: [
        [
          {
            " key ": " charset ",
            " value ": " utf- 8 "
          }
        ]
      ]
    }
  },
  components: {},
  methods: {
   importExcel() {
      this.$refs.ImportWin.setData();
    },
    save() {
      点击导入
      let files = document.getElementById("file").files[0]; //获取文件
      if (isEmpty(files)) {
        //判空处理
        messageInfo("上传文件不能为空");
        return;
      }
      let isXlsx = LimitAttach(files.name); //这里根据情况而定，是获取上传的文件名称后缀必须是xlsx
      if (isXlsx) {
        var form = new window.FormData();
        form.append("data", files); //将files的内容通过form传参给后端
        $.ajax({
          url: "", //这里是后端路径url
          type: "POST",
          data: form,
          async: true, //异步
          processData: false, //很重要，告诉jquery不要对form进行处理
          contentType: false, //很重要，指定为false才能形成正确的Content-Type
          success: function(result) {
            me.disabled = false;
            if (result === "Success") {
              messageInfo("上传成功", "success");
              this.$emit("ImportSuccess");
              this.cancle();
            } else {
              this.$alert("<div>" + result + "</div>", "警告", {
                dangerouslyUseHTMLString: true
              });
            }
          },
          error: function(jqXHR, textStatus, errorThrown) {
            var ResponseStatus = jqXHR.responseJSON.ResponseStatus;
            var Message = ResponseStatus.Message;
            this.$alert("<div>" + Message + "</div>", "警告", {
              dangerouslyUseHTMLString: true
            });
            this.InitializationObj(); //让之前上传的文件为空
            return;
          }
        });
      } else {
        this.InitializationObj();
        this.$message.error("只能上传xlsx 格式文件!");
      }
    },
    InitializationObj() {
      //这个方法是想要让选择的文件为空
      this.disabled = false;
      var fileObj = document.getElementById("file");
      if (!isEmpty(fileObj)) {
        fileObj.value = "";
      }
    },
    LimitAttach(name) {
      var ext = name.slice(name.lastIndexOf(".") + 1).toLowerCase();
      console.log(ext);
      if ("xls" == ext || "xlsx" == ext) {
        return true;
      } else {
        messageInfo("只能上传Excle文件");
        return false;
      }
    }
  },
  mounted() {},
  computed: {},
  watch: {}
}
</script>

<style lang='scss' scoped>
</style>